package cn.life.common.utils.PDF.apply;

import cn.life.business.apply.entity.po.ApplyPo;
import cn.life.common.utils.PDF.PDFBuilder;
import cn.life.common.utils.PDF.PdfUtil;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.File;
import java.io.FileOutputStream;

/**
 * @Description:    牌匾及门面装饰审查意见表
 *
 * @Author 李佳明
 * @Date 2018-10-09 16:33
 */
public class SrjTemplate {

    public boolean getPdf(String fileUrl, ApplyPo applyPo) {

        Document document = new Document();// 建立一个Document对象
        File file = new File(fileUrl);

        float height = 40f;
        float descHeight = 260f;

        try {
            /**
             *  字体
             */
            BaseFont bfChinese = BaseFont.createFont("C:/WINDOWS/Fonts/simhei.TTF", BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
            // 标题字体
            Font FontChinese18 = new Font(bfChinese, 18,com.itextpdf.text.Font.BOLD);
            // 字体格式
            Font fontChinese11Normal = new Font(bfChinese,11,Font.NORMAL);
            Font fontChinese13Normal = new Font(bfChinese,13,Font.NORMAL);

            file.createNewFile();

            Rectangle pageSize = new Rectangle(PageSize.A4);
            document.setPageSize(pageSize);
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
            PDFBuilder builder = new PDFBuilder();
            writer.setPageEvent(builder);
            document.open();

            //1.标题
            Paragraph title = new Paragraph("牌匾及门面装饰审查意见表",FontChinese18);
            title.setAlignment(PdfPCell.ALIGN_CENTER);
            title.setSpacingAfter(25);
            document.add(title);

            Paragraph no = new Paragraph("编号：" + applyPo.getApplyNo(),fontChinese11Normal);
            no.setAlignment(PdfPCell.ALIGN_RIGHT);
            no.setSpacingAfter(5);
            document.add(no);

            document.addAuthor("明远科技有限公司");

            /**
             * 初审单位与申请单位行
             */
            PdfPTable table = new PdfPTable(4); // 3 columns.
            table.setWidthPercentage(100); // Width 100%
            table.setSpacingBefore(10f); // Space before table
            table.setSpacingAfter(0); // Space after table

            // Set Column widths
            float[] columnWidths = { 1f, 2f, 1f, 2f };
            table.setWidths(columnWidths);

            PdfPCell cell1 = new PdfPCell(new Paragraph("初审单位：",fontChinese13Normal));
            cell1.setBorderColor(BaseColor.BLACK);//设置单元格边框颜色
            cell1.setMinimumHeight(height);//设置单元格高度
            cell1.setUseAscender(true); //设置可以居中
            cell1.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); //设置水平居中
            cell1.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); //设置垂直居中

            PdfPCell cell2 = new PdfPCell(new Paragraph(""));
            cell2.setBorderColor(BaseColor.BLACK);
            cell2.setMinimumHeight(height);

            PdfPCell cell3 = new PdfPCell(new Paragraph("初审时间：",fontChinese13Normal));
            cell3.setBorderColor(BaseColor.BLACK);
            cell3.setMinimumHeight(height);//设置单元格高度
            cell3.setUseAscender(true); //设置可以居中
            cell3.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); //设置水平居中
            cell3.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); //设置垂直居中

            PdfPCell cell4 = new PdfPCell(new Paragraph(""));
            cell4.setBorderColor(BaseColor.BLACK);

            PdfPCell cell5 = new PdfPCell(new Paragraph("申请单位：",fontChinese13Normal));
            cell5.setBorderColor(BaseColor.BLACK);//设置单元格边框颜色
            cell5.setMinimumHeight(height);//设置单元格高度
            cell5.setUseAscender(true); //设置可以居中
            cell5.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); //设置水平居中
            cell5.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); //设置垂直居中
            cell5.setPaddingTop(0f);

            PdfPCell cell6 = new PdfPCell(new Paragraph(""));
            cell6.setBorderColor(BaseColor.BLACK);
            cell6.setMinimumHeight(height);

            PdfPCell cell7 = new PdfPCell(new Paragraph("联系电话：",fontChinese13Normal));
            cell7.setBorderColor(BaseColor.BLACK);
            cell7.setMinimumHeight(height);//设置单元格高度
            cell7.setUseAscender(true); //设置可以居中
            cell7.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); //设置水平居中
            cell7.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); //设置垂直居中

            PdfPCell cell8 = new PdfPCell(new Paragraph(""));
            cell8.setBorderColor(BaseColor.BLACK);

            PdfPCell cell9 = new PdfPCell(new Paragraph("初审意见：", fontChinese13Normal));
            cell9.setColspan(4);// 跨4列
            cell9.setMinimumHeight(descHeight);//设置单元格高度
            cell9.setHorizontalAlignment(PdfPCell.ALIGN_LEFT); //设置水平居中
            cell9.setVerticalAlignment(PdfPCell.ALIGN_TOP); //设置垂直居中
            cell9.setPaddingLeft(12f);
            cell9.setPaddingTop(12f);

            /**
             * 终审意见
             */
            PdfPCell cell10 = new PdfPCell(new Paragraph("终审单位：",fontChinese13Normal));
            cell10.setBorderColor(BaseColor.BLACK);//设置单元格边框颜色
            cell10.setMinimumHeight(height);//设置单元格高度
            cell10.setUseAscender(true); //设置可以居中
            cell10.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); //设置水平居中
            cell10.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); //设置垂直居中

            PdfPCell cell11 = new PdfPCell(new Paragraph(""));
            cell11.setBorderColor(BaseColor.BLACK);
            cell11.setMinimumHeight(height);

            PdfPCell cell12 = new PdfPCell(new Paragraph("终审时间：",fontChinese13Normal));
            cell12.setBorderColor(BaseColor.BLACK);
            cell12.setMinimumHeight(height);//设置单元格高度
            cell12.setUseAscender(true); //设置可以居中
            cell12.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); //设置水平居中
            cell12.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); //设置垂直居中

            PdfPCell cell13 = new PdfPCell(new Paragraph(""));
            cell13.setBorderColor(BaseColor.BLACK);
            cell13.setMinimumHeight(height);

            PdfPCell cell14 = new PdfPCell(new Paragraph("终审意见：", fontChinese13Normal));
            cell14.setColspan(4);// 跨4列
            cell14.setMinimumHeight(height);//设置单元格高度
            cell14.setHorizontalAlignment(PdfPCell.ALIGN_LEFT); //设置水平居中
            cell14.setVerticalAlignment(PdfPCell.ALIGN_CENTER); //设置垂直居中
            cell14.setPaddingLeft(12f);
            cell14.setPaddingTop(12f);

            PdfPCell cell15 = new PdfPCell();
            cell15.setColspan(4);// 跨4列
            cell15.setMinimumHeight(descHeight);//设置单元格高度
            cell15.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); //设置水平居中
            cell15.setVerticalAlignment(PdfPCell.ALIGN_CENTER); //设置垂直居中

            table.addCell(cell1);
            table.addCell(cell2);
            table.addCell(cell3);
            table.addCell(cell4);
            table.addCell(cell5);
            table.addCell(cell6);
            table.addCell(cell7);
            table.addCell(cell8);
            table.addCell(cell9);
            table.addCell(cell10);
            table.addCell(cell11);
            table.addCell(cell12);
            table.addCell(cell13);
            table.addCell(cell14);
            table.addCell(cell15);

            document.add(table);

            document.close();
        } catch (Exception e) {
            document.close();
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
